package com.by.zhdoctor.web.rest;

import app.cloud.client.axis2.Client;
import com.by.zhdoctor.algorithmsUtil.AbstractStrategy;
import com.by.zhdoctor.algorithmsUtil.MatchStrategy;
import com.by.zhdoctor.algorithmsUtil.StrategyContext;
import com.codahale.metrics.annotation.Timed;
import com.by.zhdoctor.domain.ZhengXing;

import com.by.zhdoctor.repository.ZhengXingRepository;
import com.by.zhdoctor.web.rest.util.HeaderUtil;
import com.by.zhdoctor.web.rest.util.PaginationUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.inject.Inject;
import javax.persistence.EntityManager;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

/**
 * REST controller for managing ZhengXing.
 */
@RestController
@RequestMapping("/api")
public class ZhengXingResource {

    private final Logger log = LoggerFactory.getLogger(ZhengXingResource.class);

    @Inject
    private ZhengXingRepository zhengXingRepository;

    @Inject
    private EntityManager em;



    /**
     * POST  /zheng-xings : Create a new zhengXing.
     *
     * @param zhengXing the zhengXing to create
     * @return the ResponseEntity with status 201 (Created) and with body the new zhengXing, or with status 400 (Bad Request) if the zhengXing has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/zheng-xings")
    @Timed
    public ResponseEntity<ZhengXing> createZhengXing(@RequestBody ZhengXing zhengXing) throws URISyntaxException {
        log.debug("REST request to save ZhengXing : {}", zhengXing);
        if (zhengXing.getId() != null) {
            return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert("zhengXing", "idexists", "A new zhengXing cannot already have an ID")).body(null);
        }
        ZhengXing result = zhengXingRepository.save(zhengXing);
        return ResponseEntity.created(new URI("/api/zheng-xings/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert("zhengXing", result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /zheng-xings : Updates an existing zhengXing.
     *
     * @param zhengXing the zhengXing to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated zhengXing,
     * or with status 400 (Bad Request) if the zhengXing is not valid,
     * or with status 500 (Internal Server Error) if the zhengXing couldnt be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/zheng-xings")
    @Timed
    public ResponseEntity<ZhengXing> updateZhengXing(@RequestBody ZhengXing zhengXing) throws URISyntaxException {
        log.debug("REST request to update ZhengXing : {}", zhengXing);
        if (zhengXing.getId() == null) {
            return createZhengXing(zhengXing);
        }
        ZhengXing result = zhengXingRepository.save(zhengXing);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert("zhengXing", zhengXing.getId().toString()))
            .body(result);
    }

    /**
     * GET  /zheng-xings : get all the zhengXings.
     *
     * @param pageable the pagination information
     * @param filter the filter of the request
     * @return the ResponseEntity with status 200 (OK) and the list of zhengXings in body
     * @throws URISyntaxException if there is an error to generate the pagination HTTP headers
     */
    @GetMapping("/zheng-xings")
    @Timed
    public ResponseEntity<List<ZhengXing>> getAllZhengXings(Pageable pageable, @RequestParam(required = false) String filter)
        throws URISyntaxException {
        if ("chufang-is-null".equals(filter)) {
            log.debug("REST request to get all ZhengXings where chufang is null");
            return new ResponseEntity<>(StreamSupport
                .stream(zhengXingRepository.findAll().spliterator(), false)
                .filter(zhengXing -> zhengXing.getChufang() == null)
                .collect(Collectors.toList()), HttpStatus.OK);
        }
        log.debug("REST request to get a page of ZhengXings");
        Page<ZhengXing> page = zhengXingRepository.findAll(pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/zheng-xings");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

    /**
     * GET  /zheng-xings/:id : get the "id" zhengXing.
     *
     * @param id the id of the zhengXing to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the zhengXing, or with status 404 (Not Found)
     */
    @GetMapping("/zheng-xings/{id}")
    @Timed
    public ResponseEntity<ZhengXing> getZhengXing(@PathVariable Long id) {
        log.debug("REST request to get ZhengXing : {}", id);
        ZhengXing zhengXing = zhengXingRepository.findOne(id);
        return Optional.ofNullable(zhengXing)
            .map(result -> new ResponseEntity<>(
                result,
                HttpStatus.OK))
            .orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }

    /**
     * DELETE  /zheng-xings/:id : delete the "id" zhengXing.
     *
     * @param id the id of the zhengXing to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/zheng-xings/{id}")
    @Timed
    public ResponseEntity<Void> deleteZhengXing(@PathVariable Long id) {
        log.debug("REST request to delete ZhengXing : {}", id);
        zhengXingRepository.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert("zhengXing", id.toString())).build();
    }



    /**
     * GET  /zheng-xings/:bmName : get the "id" bmName.
     *
     * @param bmName the bmName of the zhengXing to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the zhengXing, or with status 404 (Not Found)
     */
    @GetMapping("/zheng-xings-byBM/{bmName}")
    @Timed
    public ResponseEntity<List<ZhengXing>> getZhengXingByBMName(@PathVariable String bmName) {
        log.debug("REST request to get ZhengXing : {}", bmName);
        List<ZhengXing> zhengXing = zhengXingRepository.findAllByBMName(bmName);
        return Optional.ofNullable(zhengXing)
            .map(result -> new ResponseEntity<>(
                result,
                HttpStatus.OK))
            .orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }



    /**
     * POST  /zheng-xings/:bmName : get the "id" bmName.
     *
     * @return the ResponseEntity with status 200 (OK) and with body the zhengXing, or with status 404 (Not Found)
     */
    @PostMapping("/zheng-xings-match/{doctorName}/{bmName}")
    @Timed
    public ResponseEntity<List<?>> getZhengXingTest(@PathVariable String doctorName,@PathVariable String bmName,@RequestBody List<String> matchList) {

        AbstractStrategy abstractStrategy= new MatchStrategy(em);
        StrategyContext strategyContext =new StrategyContext(abstractStrategy);
        List<String> list=matchList;
        List<?> r=strategyContext.contextStrategy(doctorName,bmName,list);

        return Optional.ofNullable(r)
            .map(result -> new ResponseEntity<List<?>>(
                result,
                HttpStatus.OK))
            .orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }

    /**
     * GET  /zheng-xings/:id : get the "id" zhengXing.
     *
     * @return the ResponseEntity with status 200 (OK) and with body the zhengXing, or with status 404 (Not Found)
     */
    @GetMapping("/wb")
    @Timed
    public ResponseEntity<List<String>> wb() {
        Client client=new Client();
        String msg=client.userLogin("zhdoctor01","123456").toString();
        List<String> codes = new ArrayList<>();
        codes.add(msg);
        return ResponseEntity.ok()
            .body(codes);
    }

}
